package com.itboy.weixin.servcie.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.message.config.WxAutoConfiguration;
import com.itboy.message.properties.WeChatMessageProperties;
import com.itboy.weixin.bo.WxPublicUserQueryBo;
import com.itboy.weixin.domain.WxPublicUser;
import com.itboy.weixin.dto.WxRequestOpenId;
import com.itboy.weixin.mapper.WxPublicUserMapper;
import com.itboy.weixin.servcie.WxPublicUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class WxPublicUserServiceImpl extends ServiceImpl<WxPublicUserMapper, WxPublicUser> implements WxPublicUserService {


	@Autowired
	private WeChatMessageProperties weChatMessageProperties;


	@Autowired
	private WxAutoConfiguration wxAutoConfiguration;

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private WxPublicUserService wxPublicUserService;

	@Override
	public TableDataInfo<WxPublicUser> queryPageList(WxPublicUserQueryBo wxPublicUserQueryBo) {
		IPage<WxPublicUser> page = new Page<>(wxPublicUserQueryBo.getPageNum(), wxPublicUserQueryBo.getPageSize());
		LambdaQueryWrapper<WxPublicUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StrUtil.isNotBlank(wxPublicUserQueryBo.getNickName()), WxPublicUser::getNickName, wxPublicUserQueryBo.getNickName());
		IPage<WxPublicUser> publicUserIPage = this.baseMapper.selectPage(page, queryWrapper);
		List<WxPublicUser> records = publicUserIPage.getRecords();
		for (WxPublicUser record : records) {
			record.setSubscribeTimeEx(new Date(record.getSubscribeTime()));
		}
		return new TableDataInfo(records, publicUserIPage.getTotal());
	}

	@Override
	public void syncWxPublicUser() {
		//获取关注公众号的openid
		String getUserOpenIdUrl = weChatMessageProperties.getGetUserOpenIdUrl();
		String realUrl = String.format(getUserOpenIdUrl, wxAutoConfiguration.getWxAccessToken());
		WxRequestOpenId requestOpenId = restTemplate.getForObject(realUrl, WxRequestOpenId.class);
		if (ObjectUtil.isNotNull(requestOpenId)) {
			this.recursionGet(requestOpenId);
		}
	}


	@Transactional(rollbackFor = Exception.class)
	public void recursionGet(WxRequestOpenId wxRequestOpenId) {

		//openid集合
		List<String> openIdList = wxRequestOpenId.getData().getOpenid();
		if (openIdList != null && !openIdList.isEmpty()) {

			String getUserInfoUrl = weChatMessageProperties.getGetUserInfoUrl();
			//分为100条处理
			List<List<String>> listList = Lists.partition(openIdList, 100);

			List<WxPublicUser> wxPublicUserList = new ArrayList<>();
			for (List<String> list : listList) {
				for (String openId : list) {
					String realUrl = String.format(getUserInfoUrl, wxAutoConfiguration.getWxAccessToken(), openId);
					//发送请求获取用户基本信息
					WxPublicUser wxPublicUser = restTemplate.getForObject(realUrl, WxPublicUser.class);
					//判断是否存在
					LambdaQueryWrapper<WxPublicUser> queryWrapper = new LambdaQueryWrapper<>();
					queryWrapper.eq(WxPublicUser::getOpenId, wxPublicUser.getOpenId());
					WxPublicUser publicUser = this.baseMapper.selectOne(queryWrapper);
					if (ObjectUtil.isNull(publicUser)) {
						wxPublicUserList.add(wxPublicUser);
					}
				}
				if (wxPublicUserList.size() > 0) {
					this.wxPublicUserService.saveOrUpdateBatch(wxPublicUserList);
					log.info("同步成功=>{" + wxPublicUserList + "}");
				}
			}
		}

		if (openIdList.size() >= 10000) {
			recursionGet(wxRequestOpenId);
		}

	}
}
